<template>
  <el-select
    v-model="selectedValue"
    :clearable="clearable"
    :multiple="multiple"
    :class="multiple ? 'yt-multiple' : ''"
    :placeholder="placeholder"
    @change="handleChange"
    :disabled="disabled"
  >
    <el-option
      v-for="(item, index) in items"
      :key="item.index"
      :label="item.label"
      :value="item.value"
    >
    </el-option>
  </el-select>
</template>

<script>
  export default {
    name: 'dictValueSelect',
    props: {
      selectType: {
        type: String,
        default: 'default'
      },
      placeholder: {
        type: String,
        default() {
          return this.$t('placeholder.choose')
        }
      },
      listData: {
        // 列表数据
        type: Array,
        default: () => []
      },
      multiple: {
        type: Boolean,
        default: false
      },
      codeDefault: '',
      allList: false,
      disabled: false,
      clearable: true,
      value: ''
    },
    data() {
      return {
        selectedValue: null,
        items: [],
        map: new Map()
      };
    },
    methods: {
      handleChange(val) {
        this.$emit('selectedChange', val, this.selectType);
        // const item = this.map.get(val);
        // this.$emit('change', val, item);
      },
      getItems() {
        if (this.allList) {
          if (this.listData.length > 0) {
            this.items = [];
            const obj = [{ id: 'all', label: '全部', value: '' }];
            // this.items = Object.assign(obj, this.listData)
            this.items = obj.concat(this.listData);
          }
        } else {
          this.items = [];
          this.items = Object.assign([], this.listData);
        }
      }
    },
    watch: {
      value(val) {
        if (this.multiple) {
          console.log(JSON.stringify(val));
          this.selectedValue = val;
        } else {
          this.selectedValue = val;
        }
      },
      listData() {
        this.getItems();
      },
      selectedValue(val) {
        this.$emit('input', val);
      }
    },
    mounted() {
      this.selectedValue = this.value;
      this.getItems();
    }
  };
</script>

<style scoped>
</style>
